package com.seavenois.alife2;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Init extends Activity {

	//Variable used in the confirm() function. declared global because otherwise it wont work
	boolean answer;
	
	//Buttons variable
	private Button btnNGHeaven, btnNGHell, btnLGHeaven, btnLGHell;
		
	//Check for saved data in each scenario
	private boolean saved_data(byte scenario){
		SharedPreferences savedGame = getSharedPreferences("SAVE_DEFAULT", 0);
		switch (scenario){
			case Values.SCENARIO_HEAVEN:
				savedGame = getSharedPreferences("SAVE_HEAVEN", 0);
				break;
			case Values.SCENARIO_HELL:
				savedGame = getSharedPreferences("SAVED_HELL", 0);
		}
		return savedGame.getBoolean("savedGame", false);
	}
	
	//Confirms new game if saved data
	public boolean confirm() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		AlertDialog alert;
		builder.setCancelable(false);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setTitle(getString(R.string.init_newGame));
        builder.setMessage(getString(R.string.init_newGameConfirmation));
        builder.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener(){
        	@Override
        	public void onClick(DialogInterface dialog, int which) {
        		answer = true;
        	}

        });
        builder.setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener(){
        	@Override
        	public void onClick(DialogInterface dialog, int which) {
        		answer = false;
        	}
        });
        alert = builder.create();
        alert.show();
		Log.e("ANSWER", Boolean.toString(answer));
		return answer;
	}
	
	//Starts the game
	private void startGame(byte scenario, byte mode){
		finishActivity(scenario);
		Intent gameIntent = new Intent();
		gameIntent.setComponent(new ComponentName("com.seavenois.alife2", "com.seavenois.alife2.Game"));
		if (scenario == Values.SCENARIO_HEAVEN)
			gameIntent.putExtra("heaven", true);
		else
			gameIntent.putExtra("heaven", false);
		if (mode == Values.INIT_LOAD)
			gameIntent.putExtra( "load", true);
		else
			gameIntent.putExtra( "load", false);
		startActivityForResult(gameIntent, scenario);
		gameIntent = null;
		//Check for saved data and enable load game buttons
        if (saved_data(Values.SCENARIO_HEAVEN))
        	btnLGHeaven.setEnabled(true);
        if (saved_data(Values.SCENARIO_HELL))
        	btnLGHell.setEnabled(true);
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_init);
        
        //Assign buttons
        btnNGHeaven = (Button) findViewById(R.id.init_btnNGHeaven);
        btnNGHell = (Button) findViewById(R.id.init_btnNGHell);
        btnLGHeaven = (Button) findViewById(R.id.init_btnLGHeaven);
        btnLGHell = (Button) findViewById(R.id.init_btnLGHell);
                
        //Check for saved data and enable load game buttons
        if (saved_data(Values.SCENARIO_HEAVEN))
        	btnLGHeaven.setEnabled(true);
        if (saved_data(Values.SCENARIO_HELL))
        	btnLGHell.setEnabled(true);
        
        //Set onClick events
        //If loading game, do
        btnLGHeaven.setOnClickListener(new OnClickListener(){
        	public void onClick(View v) {
        		startGame(Values.SCENARIO_HEAVEN, Values.INIT_LOAD);
			}
        });
        btnLGHell.setOnClickListener(new OnClickListener(){
        	public void onClick(View v) {
        		startGame(Values.SCENARIO_HELL, Values.INIT_LOAD);
			}
        });
        //TODO: If new game, check for saved data for the scenario
        //If found, ask for confirmation
        btnNGHeaven.setOnClickListener(new OnClickListener(){
        	public void onClick(View v) {
        		/*if(saved_data(Values.SCENARIO_HEAVEN) == true){
        			if(confirm() == true)
        				startGame(Values.SCENARIO_HEAVEN, Values.INIT_NEW);
        		}
        		else*/
        			startGame(Values.SCENARIO_HEAVEN, Values.INIT_NEW);
			}
        });    
        btnNGHell.setOnClickListener(new OnClickListener(){
    		public void onClick(View v) {
        		/*if(saved_data(Values.SCENARIO_HELL) == true){
        			if(confirm() == true)
        				startGame(Values.SCENARIO_HELL, Values.INIT_NEW);
        		}
        		else*/
        			startGame(Values.SCENARIO_HELL, Values.INIT_NEW);
			}
        });    
    }

    //Load xml for the options menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_init, menu);
        return true;
    }
    
    //Check selected menu item and start selected activity
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent intent = new Intent();
    	switch (item.getItemId()){
    		case R.id.init_menu_settings:
    			intent.setClass(Init.this, SetPreferenceActivity.class);
    	    	startActivityForResult(intent, 0);
    	    	break;
    		case R.id.init_menu_cheats:
    			intent.setClass(Init.this, SetCheatsActivity.class);
    	    	startActivityForResult(intent, 0);
    	    	break;
    		case R.id.init_menu_changelog:
    			intent.setComponent(new ComponentName("com.seavenois.alife2", "com.seavenois.alife2.Changelog"));
    			startActivity(intent);
    			break;
    		case R.id.init_menu_donate:
    			intent.setComponent(new ComponentName("com.seavenois.alife2", "com.seavenois.alife2.Donate"));
    			startActivity(intent);
    			break;
    	} 
     
        return true;
    }

    //Load preferences.
    //TODO: I don't exactly remember how does this work, but it works
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {    
		//To make it simple, always re-load Preference setting.
		loadPref();
	}
       
    private void loadPref(){
    	//TODO: Implement this (whatever is that this do)
    	//SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    	//String my_edittext_preference = mySharedPreferences.getString("edittext_preference", "");
    	//prefEditText.setText(my_edittext_preference);
    }
}
